﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.Collapse
{
    public partial class TableStatsEditor : Form,IComponentEditor
    {
        private object _ownerActivityNode;
        private TableStatsComponent _TableStats;

        public TableStatsEditor()
        {
            InitializeComponent();
        }

        private void TableStatsEditor_Load(object sender, EventArgs e)
        {

        }

        #region IComponentEditor Members

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        /// <summary></summary>
        public object OwnerActivityNode
        {
            get { return _ownerActivityNode; }
            set { _ownerActivityNode = value; }
        }

        /// <summary></summary>
        public Type WorkflowComponentType
        {
            get { return typeof(TableStatsComponent); }
        }

        /// <summary></summary>
        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents,
                                 object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ownerActivityNode = activityNode;
            this._TableStats = (TableStatsComponent) wfComponent;

            this.txt_ConnStr.Text = this._TableStats.ConnStr;
            if(this.cbo_Tables.Items.Count>0 && (!string.IsNullOrEmpty(this._TableStats.TableName)))
            {
                for(int i=0;i<this.cbo_Tables.Items.Count;i++)
                {
                    if(this.cbo_Tables.Items[i].ToString()==this._TableStats.TableName)
                    {
                        this.cbo_Tables.SelectedIndex = i;
                        break;
                    }
                }
            }
            else
            {
                this.cbo_Tables.Text = this._TableStats.TableName;
            }
        }

        /// <summary></summary>
        public void SaveSettings()
        {
            this._TableStats.ConnStr = this.txt_ConnStr.Text;
            this._TableStats.TableName = this.cbo_Tables.Text;
        }

        /// <summary></summary>
        public TestResult Test()
        {
            return new TestResult(true,"Succeed");
        }

        #endregion

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(this.txt_ConnStr.Text))
            {
                string[] tbls = DbUtil.GetTables(this.txt_ConnStr.Text);
                this.cbo_Tables.Items.Clear();
                if(tbls !=null && tbls.Length>0)
                {
                    foreach(string tblName in tbls)
                    {
                        this.cbo_Tables.Items.Add(tblName);
                    }
                }
                this.cbo_Tables.SelectedIndex = -1;
                this.cbo_Tables.Text = string.Empty;

                if(!string.IsNullOrEmpty(this._TableStats.TableName))
                {
                    for (int i = 0; i < this.cbo_Tables.Items.Count; i++)
                    {
                        if (this.cbo_Tables.Items[i].ToString() == this._TableStats.TableName)
                        {
                            this.cbo_Tables.SelectedIndex = i;
                            break;
                        }
                    }
                }
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if(this.UpdateComponentSettingEvent !=null)
            {
                this.UpdateComponentSettingEvent(this._ownerActivityNode, this._TableStats);
            }
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
